import {Injectable} from '@angular/core';
import {NzMessageService} from 'ng-zorro-antd';
import {ConfigService} from '@kylin/config';
import {HttpService} from '@kylin/http';

@Injectable()
export class DownloadService {
    constructor(private message: NzMessageService,
                private config: ConfigService,
                private http: HttpService
    ) {
    }

    download(url) {
        let path: string;
        if (url.startsWith('http')) {
            path = url;
        } else {
            path = this.config.imgHostname + url;
        }
        const eleLink = document.createElement('a');
        eleLink.setAttribute('target', '_Blank');
        eleLink.download = '';
        eleLink.style.display = 'none';
        eleLink.href = this.config.imgHostname + path + '?contentDisposition=attachment';
        document.body.appendChild(eleLink);
        eleLink.click();
        document.body.removeChild(eleLink);
    }
}
